import { getToken } from "@/utils/auth";
import axios from "axios";
const config = {
  baseURL: import.meta.env.VITE_BASE_API,
  timeout: 3000
};

const services = axios.create(config);

//发送请求时，在请求头中设置token
services.interceptors.request.use(
  (config) => {
    const token = getToken();
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

//响应报文的拦截
services.interceptors.response.use(
  (response) => {
    // code
    if (response.status === 200) {
      return response.data;
    }
  },
  (err) => {
    return Promise.reject(err);
  }
);
export default services;
